package vn.gq.tym.storytemplate;

import java.util.ArrayList;
import java.util.List;
import vn.gq.tym.model.ChapterItemAdapter;
import vn.gq.udv.db.items.ChapterItem;
import vn.gq.udv.utils.AdmobAdsHandler;
import vn.gq.udv.utils.Bookmark;
import vn.gq.udv.utils.ButtonHelper;
import vn.gq.udv.utils.SQLiteDatabaseVersionHandler;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.view.MenuItem;
import com.google.ads.AdView;
import com.google.analytics.tracking.android.EasyTracker;

public class ChapterActivity extends BaseActivity {

	private ListView mListView;
	private TextView chapterNotification;
	private List<ChapterItem> mChapterList = new ArrayList<ChapterItem>();
	private int mCatId = 0;
	private String mCatName = "";
	private Bookmark bookmark;
	private AdmobAdsHandler mAdHandler;
	private String trackUri = "/chap";
	private ChapterItemAdapter mAdapter;
	private ProgressBar mProgressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.chapter_scr);
		String author = getString(R.string.author_name);
		if (!author.trim().equals(""))
			getSupportActionBar().setSubtitle(author);
		new SQLiteDatabaseVersionHandler(this).checkDBVersionAndUpgrade();
		try { 
			mCatId = getIntent().getExtras().getInt("CATID"); 
			mCatName = getIntent().getExtras().getString("CATNAME");
		} catch(NullPointerException e) { 
			mCatId = 0;
			mCatName = "";
		}
		// track using google analytics
		if (!mCatName.equals("")) {
			trackUri = String.format("/cat/%s/chap", mCatName);
		} 
		GAManager.newInstance(this).track(getString(R.string.app_name)+trackUri);
		mListView = (ListView) findViewById(R.id.chapterlist);
		mListView.setVisibility(View.GONE);
		mProgressBar = (ProgressBar) findViewById(R.id.progress);
		mProgressBar.setVisibility(View.VISIBLE);
		
		if (!GQConst.FINISHED) {
			ViewGroup vgr = (ViewGroup) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_list_footer, null, false);
			mListView.addFooterView(vgr);
		}
		
		chapterNotification = (TextView) findViewById(R.id.tvnotification);
		chapterNotification.setSelected(true);
		bookmark = new Bookmark(ChapterActivity.this, chapterNotification);
		mAdapter = new ChapterItemAdapter(ChapterActivity.this);
		mAdapter.load(mCatId, new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mListView.setVisibility(View.VISIBLE);
				mProgressBar.setVisibility(View.GONE);
				bookmark.setData(mAdapter.getData());
				mChapterList = mAdapter.getData();
			}
		});
		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (id != -1) {
					ChapterItem chapter = mChapterList.get(position);
					chapter.setPosition(position);
//					ReaderActivity.startReaderActivity(ChapterActivity.this, (ArrayList<? extends Parcelable>) mChapterList, position, chapter.getCatId());
					StoryReaderActivity.startActivity(ChapterActivity.this, (ArrayList<ChapterItem>) mChapterList, position);
				}
			}
		});
		mAdHandler = new AdmobAdsHandler((AdView) findViewById(R.id.adView));
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
		bookmark.showNotificationOnStartUp();
		mAdHandler.loadAds();
	}
	
	public static void startActivity(Context context, int catId, String catName) {
		Intent intent = new Intent(context, ChapterActivity.class);
		intent.putExtra("CATID", catId);
		intent.putExtra("CATNAME", catName);
		context.startActivity(intent);
	}
	
	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		getSupportMenuInflater().inflate(R.menu.index, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case android.R.id.home:
				onBackPressed();
				break;
			case R.id.menu_info:
				new ButtonHelper(ChapterActivity.this).showMoreApp();
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		mAdHandler.onStop();
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onDestroy() {
		mAdHandler.onDestroy();
		super.onDestroy();
	}
}
